#!/bin/bash
#
# jHiccupLogProcessor
#
# Written by Gil Tene, and released to the public domain, as explained at
#  http://creativecommons.org/publicdomain/zero/1.0/
#
JHICCUP_Version=2.0.4
#
# jHiccupLogProcessor will process an input log and can generate two
# different log files from a single jHiccup histogram log file: a
# sequential interval log file and a histogram log file.
#
# The sequential interval log file logs a single %'ile stats line for
# each reporting interval.
#
# The histogram percentile log file includes a detailed %'ile histogram
# of the entire log file range.
#
# jHiccupLogProcessor will process an input log file when provided with
# the -i <filename> option. When no -i option is provided, standard input
# will be processed.
#
# When provided with an output file name <logfile> with the -o option
# (e.g. "-o mylog"), jHiccupLogProcessor will produce both output files
# under the names <logfile> and <logfile>.hgrm (e.g. mylog and mylog.hgrm).
#
# When not provided with an output file name, jHiccupLogProcessor will
# produce [only] the histogram percentile log output to standard output.
#
# jHiccupLogProcessor accepts optional -start and -end time range
# parameters. When provided, the output will only reflect the portion
# of the input log with timestamps that fall within the provided start
# and end time range parameters.
#
# jHiccupLogProcessor also accepts and optional -csv parameter, which
# will cause the output formatting (of both output file forms) to use
# a CSV file format.
#

# Figure out installed path:
# On Linux, we'd do the following:
# PARSED_SCRIPT=`readlink -f $0`
# INSTALLED_PATH=`dirname $PARSED_SCRIPT`
# But readlink -f doesn't work the same everywhere (e.g. Mac OS). We use this instead:
readlink_f() {
  local _dir _path="$1"
  [ -d "$_path" ] && _path=$(cd "$_path"; pwd -P)
  while [ -L "$_path" ]; do
    _dir=$(dirname "$_path")
    _path=$(readlink "$_path")
    [ -n "${_path##/*}" ] && _path="${_dir}/${_path}"
  done
  [ -f "$_path" ] && _path="$(cd "$(dirname "$_path")"; pwd -P)/${_path##*/}"
  echo "${_path%/}"
}

INSTALLED_PATH=$(dirname "$(readlink_f "$0")")

# Check if running from unpacked distribution archive by assuming jHiccup.jar
# in the same directory as this script. If not, try to search in target/ directory
# (running from the source repository build).
JHICCUP_JAR_FILE="$INSTALLED_PATH/jHiccup.jar"
if [ ! -f "$JHICCUP_JAR_FILE" ] ; then
  JHICCUP_JAR_FILE="$INSTALLED_PATH/target/jHiccup.jar"
fi

if [ ! -f "$JHICCUP_JAR_FILE" ] ; then
  JHICCUP_JAR_FILE="$INSTALLED_PATH/../jHiccup.jar"
fi

if [ ! -f "$JHICCUP_JAR_FILE" ] ; then
    echo "For this command to run, jHiccup.jar must be available in '${INSTALLED_PATH%/bin*}'."
    exit 1
fi

JAVA_BIN=`which java`

if [ $JAVA_HOME ]; then
    JAVA_CMD=$JAVA_HOME/bin/java
elif [ $JAVA_BIN ]; then
    JAVA_CMD=$JAVA_BIN
else
    echo "For this command to run, either $JAVA_HOME must be set, or java must be in the path."
    exit 1
fi

#
# Parse original java execution arguments:
#
# At this point, we should have valid $PARSED_BinJava, $PARSED_JavaArgs, $PARSED_AppArgs:
#echo PARSED_BinJava = "$PARSED_BinJava"
#echo PARSED_JavaArgs = "$PARSED_JavaArgs"
#echo PARSED_AppArgs = "$PARSED_AppArgs"

# Deal with Windows/cygwin path normalization syntax needs:
# Key Assumption: only cygwin/Windows installations will have a cygpath command...
cygpath -w $JHICCUP_JAR_FILE &> /dev/null
if [ $? -eq 0 ] ; then
    # if using cygwin, use valid windows-style classpath
    JHICCUP_JAR_FILE=`cygpath -w $JHICCUP_JAR_FILE`
	echo Windows path for hiccup jar file is $JHICCUP_JAR_FILE
fi

exec $JAVA_CMD -cp $JHICCUP_JAR_FILE org.jhiccup.internal.hdrhistogram.HistogramLogProcessor $@
#exec $CMD
